import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ClientThread implements Runnable{
	private Socket client;
	private Database db;
	private WriteThread writeThread;
	private Parser parser;
	
	
	public ClientThread(Socket client, Database db, WriteThread writeThread) {
		this.client = client;
		this.db = db;
		this.writeThread = writeThread;
		
	}

	@Override
	public void run() {
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
			String inputLine;
			
			parser = new Parser();
	        while ((inputLine = in.readLine()) != null) {
				inputLine = inputLine.trim();
				parser.parseLine(inputLine);
				
				if(inputLine.startsWith("</MEASUREMENT>")) {
					Measurement measurement = parser.getMeasurement();
					int stn = measurement.getStn();
					Station station = null;
					if(Server.stations.containsKey(stn) == false) {
						station = new Station();
						Server.stations.put(stn, station);
						//System.out.println("New station added: " + stn);
					}
					else {
						station = Server.stations.get(stn);
					}
					
					ArrayList<String> missing = parser.getMissing();
//					if(!station.isHistoryFilled()) {
//						//Search for station data
//						ResultSet rs = db.select("SELECT * FROM measurement " +
//								"WHERE stn=" + stn + " " +
//								"LIMIT 30;");
//						try {
//							//Check the number of rows
//							rs.last();
//							int results = rs.getRow();
//							rs.first();
//							
//							if(results >= 30) {
//								while(rs.next()) {
//									Measurement m = new Measurement();
//									m.setStn(rs.getInt(2));
//									m.setDate(rs.getString(3));
//									m.setTime(rs.getString(4));
//									m.setTemp(rs.getFloat(5));
//									m.setDewp(rs.getFloat(6));
//									m.setStp(rs.getFloat(7));
//									m.setSlp(rs.getFloat(8));
//									m.setVisib(rs.getFloat(9));
//									m.setWdsp(rs.getFloat(10));
//									m.setPrcp(rs.getFloat(11));
//									m.setSndp(rs.getFloat(12));
//									m.setFrshtt(rs.getInt(13));
//									m.setCldc(rs.getFloat(14));
//									m.setWnddir(rs.getInt(15));
//									station.addMeasurement(m);
//								}
//								//System.out.println("Station recieved: " + stn + ", number of stations: " + Server.stations.size());
//							}
//						} catch (SQLException e) {
//							e.printStackTrace();
//						}
//						
//					}
					
					if(station.isHistoryFilled()) {
						
						//Predict missing values
						for(String s : missing) {
							System.out.println("Missing value " + s + " found.");
							station.predict(s, measurement);
	
						}
						//Check 20% off temperatures
						station.checkTemp(measurement);
						
						//add to database and stationhistory
						station.addMeasurement(measurement);
						
						//There is no more missing data
						missing.clear();
						
					}
					
					if(missing.isEmpty()) {
						//add to database and stationhistory
						station.addMeasurement(measurement);
						//System.out.println(stn);
						writeThread.add(measurement);
					}
					else {
						System.out.println("Measurement cannot be inserted, there is missing data and there is no history of >= 30");
					}
				}				
	        }
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
